package com.ruoyi.web.controller.carte;

import java.util.List;

import com.ruoyi.system.domain.request.CarteStatusRequest;
import com.ruoyi.system.service.carte.server.ICarteStatus;
import com.ruoyi.system.xxkutil.XxkUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.XxkCarte;
import com.ruoyi.system.service.IXxkCarteService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * carte服务器Controller
 * 
 * @author ruoyi
 * @date 2024-02-28
 */
@Controller
@RequestMapping("/system/carte")
public class XxkCarteController extends BaseController
{
    private String prefix = "system/carte";

    @Autowired
    private IXxkCarteService xxkCarteService;

    @Autowired
    private ICarteStatus carteStatus;


    @RequiresPermissions("system:carte:view")
    @GetMapping()
    public String carte()
    {
        return prefix + "/carte";
    }

    /**
     * 查询carte服务器列表
     */
    @RequiresPermissions("system:carte:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(XxkCarte xxkCarte)
    {
        startPage();
        List<XxkCarte> list = xxkCarteService.selectXxkCarteList(xxkCarte);
        return getDataTable(list);
    }

    /**
     * 导出carte服务器列表
     */
    @RequiresPermissions("system:carte:export")
    @Log(title = "carte服务器", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XxkCarte xxkCarte)
    {
        List<XxkCarte> list = xxkCarteService.selectXxkCarteList(xxkCarte);
        ExcelUtil<XxkCarte> util = new ExcelUtil<XxkCarte>(XxkCarte.class);
        return util.exportExcel(list, "carte服务器数据");
    }

    /**
     * 新增carte服务器
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存carte服务器
     */
    @RequiresPermissions("system:carte:add")
    @Log(title = "carte服务器", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XxkCarte xxkCarte)
    {
        return toAjax(xxkCarteService.insertXxkCarte(xxkCarte));
    }

    /**
     * 修改carte服务器
     */
    @RequiresPermissions("system:carte:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        XxkCarte xxkCarte = xxkCarteService.selectXxkCarteById(id);
        mmap.put("xxkCarte", xxkCarte);
        return prefix + "/edit";
    }

    /**
     * 修改保存carte服务器
     */
    @RequiresPermissions("system:carte:edit")
    @Log(title = "carte服务器", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XxkCarte xxkCarte)
    {
        return toAjax(xxkCarteService.updateXxkCarte(xxkCarte));
    }

    /**
     * 删除carte服务器
     */
    @RequiresPermissions("system:carte:remove")
    @Log(title = "carte服务器", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(xxkCarteService.deleteXxkCarteByIds(ids));
    }



    @RequiresPermissions("system:carte:edit")
    @GetMapping("/serverInfo/{id}")
    public String serverInfo(@PathVariable("id") Long id,ModelMap mmap)
    {
        XxkCarte xxkCarte = xxkCarteService.selectXxkCarteById(id);
        CarteStatusRequest request = new CarteStatusRequest();
        request.setCarteCode(xxkCarte.getCarteCode());
        String result = carteStatus.status(request);
        String baseUrl = "http://"+xxkCarte.getCarteIp()+":"+xxkCarte.getCartePort();
        String modifiedHtml = XxkUtils.replaceRelativeUrls(result, baseUrl);
        modifiedHtml = modifiedHtml.replaceAll("/kettle", baseUrl + "/kettle");
        mmap.put("serverInfo",modifiedHtml);
        return prefix + "/carteServerInfo";
    }


}
